Release 10.1A: OpenEdge Development:
Programming Interfaces
Accessing and incrementing sequences
Figure 1–8 lists Progress statements and functions you can use to access and increment sequence values from the 4GL.
Whenever a
CURRENT–VALUEstatement orNEXT–VALUEfunction changes the value of a sequence, the new value persists in the database where the sequence is defined until it is changed again, or the sequence is deleted from the database.The sequence statements and functions have the following syntax:
sequenceAn identifier that specifies the name of a sequence defined in the Data Dictionary. Note that a sequence can have the same name as a database field, but they are distinct entities.
logical-dbnameAn identifier that specifies the logical name of the database in which the sequence is defined. The database must be connected. You can omit this parameter only if you have a single database connected. Otherwise, you must specify the database in which the sequence is defined.
expressionAn expression that evaluates to an integer value. The
CURRENT–VALUEstatement assigns the value ofexpressionto the specified sequence. The value ofexpressionmust be within the range defined for the specified sequence.The dynamic sequence statement and functions have the same general syntax, but allow you to specify the sequence and database dynamically at run time. Their syntax is as follows:
sequence-expressionA character expression which evaluates to the name of a sequence defined in the Data Dictionary. Note that a sequence can have the same name as a database field, but they are distinct entities.
logical-dbname-expressionA character expression which evaluates to the logical name of the database in which the sequence is defined. The database must be connected. This argument is required and cannot be Unknown value (
?).expressionAn expression that evaluates to an integer value. The
DYNAMIC–CURRENT–VALUEstatement assigns the value ofexpressionto the specified sequence. The value ofexpressionmust be within the range defined for the specified sequence.In general, use the
CURRENT–VALUEandNEXT–VALUEfunctions for mission-critical applications that depend on access to reliable and orderly sequence values. Use theCURRENT–VALUEstatement only for database maintenance and initialization, primarily during the development cycle. For more information on these functions and statements, see OpenEdge Development: Progress 4GL Reference .Using the CURRENT–VALUE function
Use the
CURRENT–VALUEfunction in any integer expression to retrieve the current value of a sequence without incrementing it. The current value of a sequence can be any one of the following:The following example gets the most recent customer number in the default database (maintained by the cust–num sequence), and displays each order record for that customer:
Using the NEXT–VALUE function
Use the
NEXT–VALUEfunction to increment a sequence by its defined positive or negative increment value. If the sequence cycles andNEXT–VALUEincrements it beyond its Upper or Lower limit, the function sets and returns the defined Initial value for the sequence. If the sequence terminates andNEXT–VALUEtries to increment it beyond its Upper or Lower limit, the function returns the Unknown value (?) and leaves the sequence value unchanged.The following example creates a new customer record with the next available customer number generated by the cust-num sequence:
Because this example does not check the cust-num sequence for termination, it implies that cust-num is a cycling sequence. Because it does check for and ignore existing records containing the generated cust-num value, the example can reuse previously deleted (or otherwise skipped) customer numbers after the sequence cycles.
Using the CURRENT–VALUE statement
Use the
Caution: Avoid using this statement in mission-critical applications. Use of this statement, especially in a multi-user context, can compromise the referential integrity of your database. Any database application designed to rely on the orderly values provided by sequences cannot reliably reset existing database fields according to potentially unexpected sequence values.CURRENT–VALUEstatement to explicitly set a sequence to a new value. You can assign the defined initial value of a sequence, its upper or lower limit, or any integer value in between. Trying to set a value outside these bounds causes an error. Note that you cannot assign the Unknown value (?) to a sequence. Unlike theNEXT–VALUEfunction, theCURRENT–VALUEstatement always sets a new sequence value in a transaction, starting one, if necessary.The purpose of the
CURRENT–VALUEstatement is to maintain a database off-line, under program control. Note that the Data Dictionary uses this statement when you create a new sequence to set the initial value. Possible uses include:Keep in mind that all such uses must respect and might require changes to the _Sequence table. This table contains one record for each sequence defined in the database. Creating a record in this table automatically allocates and assigns the data storage for a new sequence. Deleting a record in this table automatically releases the data storage for the specified sequence.
Progress provides fault detection that prevents a sequence from being created with inconsistent attribute values in the _Sequence table (such as, the minimum value greater than the maximum value). It also prevents the assignment of a new current value that is inconsistent with the corresponding attribute values in the _Sequence table (such as, a current value outside the minimum and maximum value boundary).
The following code fragment sets a new current value for sequence used for testing:
Progress also uses this statement in the Data Administration tool to load sequence definitions and values from dump-formatted text files. You can accomplish the same task implemented in the code fragment above using this tool. For more information, see OpenEdge Data Management: Database Administration .
Compiling procedures that reference sequences
When compiling procedures that use sequence statements and functions, do not run OpenEdge with the Time Stamp (-
tstamp) startup parameter. All references toCURRENT-VALUEandNEXT–VALUEstatements and functions in the r-code depend on CRC calculations made with the _Sequence metaschema table, and are not available with time stamping. CRC validation is now the default data consistency option for OpenEdge.Resetting a terminating sequence
When a terminating sequence stops at its upper or lower limit, you can reset it by assigning a valid sequence value to it with the
CURRENT–VALUEstatement. You can also reset a stopped terminating sequence by changing it to a cycling sequence. The first use of theNEXT–VALUEfunction for the new cycling sequence resets the sequence to its initial value.Referencing sequences within a WHERE clause
You cannot invoke sequence functions from within a
WHEREclause. This generates a compiler error, because sequence expressions do not participate in the index resolution and optimization phase of the Compiler.If you want to use a sequence value within a
WHEREclause, set a variable or field to the sequence value, and reference the variable or field in yourWHEREclause. See the sections that describe each sequence function for examples.
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |